// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2023.
 * Description: notifier: add task exit notifier chain
 * Author: liuchao
 * Create: 2020-05-08
 */

#ifdef CONFIG_EULEROS_TASK_EXIT_NOTIFIER_CHAIN
#include <linux/task_exit_notifier.h>

static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
#ifdef CONFIG_RTOS_TASK_EXIT_NOTIFIER_CHAIN
int __task_exit __read_mostly = 1;
#else
int __task_exit __read_mostly;
#endif

int task_exit_notifier_register(struct notifier_block *nb)
{
	return blocking_notifier_chain_register(&task_exit_notifier, nb);
}
EXPORT_SYMBOL_GPL(task_exit_notifier_register);

int task_exit_notifier_unregister(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(&task_exit_notifier, nb);
}
EXPORT_SYMBOL_GPL(task_exit_notifier_unregister);

void last_task_exit(struct task_struct *task)
{
	if (__task_exit)
		blocking_notifier_call_chain(&task_exit_notifier, 0, task);
}

static int __init parse_task_exit(char *arg)
{
	if (!arg)
		return -EINVAL;

	if (!strcasecmp(arg, "1"))
		__task_exit = 1;
	else
		__task_exit = 0;

	pr_info("task_exit: %d\n", __task_exit);
	return 0;
}
early_param("task_exit_notifier", parse_task_exit);

int task_exit_notifier_enabled(void)
{
	return __task_exit;
}
EXPORT_SYMBOL(task_exit_notifier_enabled);

#endif
